package com.xuelang.pipeline.worker.app.secret.impl;

import com.xuelang.pipeline.worker.app.common.util.K8sUtils;
import com.xuelang.pipeline.worker.app.common.util.VerifyUtil;
import com.xuelang.pipeline.worker.app.secret.SecretService;
import com.xuelang.pipeline.worker.client.crd.dto.SecretDTO;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Secret;
import io.kubernetes.client.openapi.models.V1SecretList;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @author liuyuan.ly
 * @since 2024/7/8 15:51
 */
@Service
@Slf4j
public class SecretServiceImpl implements SecretService {


    public V1Secret createSecret(SecretDTO secretDTO) {
        K8sUtils.initApiClient(secretDTO.getKubeConfig());
        V1Secret v1Secret = null;
        CoreV1Api api = new CoreV1Api();
        try {
            v1Secret = api.createNamespacedSecret(secretDTO.getNamespaceName(), secretDTO.getV1Secret(), null, null, null, null);
        } catch (ApiException e) {
            log.info("创建secret失败:{},堆栈信息:", e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }
        return v1Secret;
    }


    @Override
    public V1Secret saveOrUpdateSecret(SecretDTO secretDTO) {
        VerifyUtil.verifyStringNotBlank(secretDTO.getSecretName(), "secret名字不能为空");
        V1SecretList v1SecretList = getSecretListByName(secretDTO);
        CoreV1Api api = new CoreV1Api();
        V1Secret v1Secret;
        if (!v1SecretList.getItems().isEmpty()) {
            V1Secret secret = v1SecretList.getItems().get(0);
            secret.setData(secretDTO.getData());
            try {
                v1Secret = api.replaceNamespacedSecret(secretDTO.getSecretName(), secretDTO.getNamespaceName(), secret, null, null, null, null);
            } catch (ApiException e) {
                log.info("更新secret失败:{},堆栈信息:", e.getResponseBody(), e);
                throw new RuntimeException(e.getResponseBody());
            }
        } else {
            v1Secret = createSecret(secretDTO);
        }
        return v1Secret;
    }

    @Override
    public V1SecretList getSecretListByName(SecretDTO secretDTO) {
        K8sUtils.initApiClient(secretDTO.getKubeConfig());
        String filedName = "";
        if (!StringUtils.isBlank(secretDTO.getSecretName())) {
            filedName = "metadata.name=" + secretDTO.getSecretName();
        }
        CoreV1Api api = new CoreV1Api();
        V1SecretList v1SecretList;
        try {
            v1SecretList = api.listNamespacedSecret(secretDTO.getNamespaceName(), null, null,
                    null, filedName, null, null, null, null,
                    null, null);
        } catch (ApiException e) {
            log.info("查询secret失败:{},堆栈信息:", e.getResponseBody(), e);
            throw new RuntimeException(e.getResponseBody());
        }
        return v1SecretList;
    }


}
